/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package diet.dao;

import diet.dto.DishDetailDTO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 *
 * @author DungTT
 */
public class DishDetailDAO {

    public boolean add(DishDetailDTO dishdetail) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = DAO.ConnectWithDriver();
            stm = con.prepareStatement("INSERT INTO [DishDetail](DishID,FoodID,Ammount) VALUES(?,?,?)");
            stm.setInt(1, dishdetail.getDishID());
            stm.setInt(2, dishdetail.getFoodID());
            stm.setFloat(3, dishdetail.getAmmount());
            stm.executeUpdate();
            return true;
        } catch (SQLException ex) {
            System.out.println("SQLException Error: " + ex.getMessage());
            return false;
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    System.out.println("Connection error: " + ex.getMessage());
                }
            } else if (stm != null) {
                try {
                    stm.close();
                } catch (SQLException ex) {
                    System.out.println("Statement error " + ex.getMessage());
                }
            }
        }
    }

    public boolean delete(int DishDetailID) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = DAO.ConnectWithDriver();
            stm = con.prepareStatement("DELETE FROM [DishDetail] WHERE DishDetailID=? ");
            stm.setInt(1, DishDetailID);
            stm.executeUpdate();
            return true;
        } catch (SQLException ex) {
            System.out.println("SQLException Error: " + ex.getMessage());
            return false;
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    System.out.println("Connection error: " + ex.getMessage());
                }
            } else if (stm != null) {
                try {
                    stm.close();
                } catch (SQLException ex) {
                    System.out.println("Statement error " + ex.getMessage());
                }
            }
        }
    }

    public boolean update(DishDetailDTO dishdetail) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = DAO.ConnectWithDriver();
            stm = con.prepareStatement("UPDATE [DishDetail] SET DishDetailID=?,DishID=?,FoodID=?,Ammount=?"
                    + " WHERE DishDetailID=?");
            stm.setInt(1, dishdetail.getDishID());
            stm.setInt(2, dishdetail.getFoodID());
            stm.setFloat(3, dishdetail.getAmmount());
            stm.executeUpdate();
            return true;
        } catch (SQLException ex) {
            System.out.println("SQLException Error: " + ex.getMessage());
            return false;
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    System.out.println("Connection error: " + ex.getMessage());
                }
            } else if (stm != null) {
                try {
                    stm.close();
                } catch (SQLException ex) {
                    System.out.println("Statement error " + ex.getMessage());
                }
            }
        }
    }
}
